<template>
  <view class="mobile-item-container">
	<uni-section :title="id ? '修改' : '新增'" type="line">
		<view class="example">
			<uni-forms ref="form" :model="formData" :rules="rules" labelWidth="80px">
				<uni-forms-item label="水果名称" required name="fruitsId">
					<uni-data-select v-model="formData.fruitsId" :localdata="fruitsInfoList" >
					</uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="进货日期" required name="salesTime">
					<uni-datetime-picker type="date" return-type="timestamp"  v-model="formData.salesTime" />
				</uni-forms-item>
				
				<uni-forms-item label="进货数量" required name="stockNum">
					<uni-easyinput v-model="formData.stockNum" placeholder="请输入销售总额" />
				</uni-forms-item>
				
				<uni-forms-item label="进货价"  required name="purchasePrice">
					<uni-easyinput v-model="formData.purchasePrice" placeholder="请输入进货价" />
				</uni-forms-item>
				
				<uni-forms-item label="零售价"  required name="retailPrice">
					<uni-easyinput v-model="formData.retailPrice" placeholder="请输入零售价" />
				</uni-forms-item>
				 
				<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="formData.remark" placeholder="请输入备注" />
				</uni-forms-item>
			</uni-forms>
			<view class="button-group">
				<u-row :gutter="12">
				  <u-col :span="4">
				    <u-button v-if="id" type="error" text="删除" @click="del" v-hasPermi="['fruits:FruitsStockInfo:remove']"></u-button>
				  </u-col>
				  <u-col :span="4">
				    <u-button icon="arrow-left" text="返回" plain @click="goBack()"></u-button>
				  </u-col>
				  <u-col :span="4">
					<u-button v-if="!id" type="primary" text="新增" @click="submit" v-hasPermi="['fruits:FruitsStockInfo:add']"></u-button>
					<u-button  v-else type="primary" text="修改" @click="submit" v-hasPermi="['fruits:FruitsStockInfo:edit']"></u-button>
				  </u-col>
				</u-row>
			</view>
		</view>
	</uni-section>

	<buttom-tabbar></buttom-tabbar>
  </view>
</template>

<script>
import * as FruitsStockInfoApi from '@/api/fruits/FruitsStockInfo'
import * as FruitsInfoApi from '@/api/fruits/fruitsInfo'
import Navbar from '@/components/navbar/Navbar'
import * as DictInfoApi from '@/api/system/dict/data'

export default {
  dicts: ['unit'],
  components: {
    Navbar,
  },
  data () {
    return {
      id: undefined,
      formData:{
             id: null,
			 fruitsId: null,
			 salesTime: null,
			 purchasePrice: 0,
			 retailPrice: 0,
			 stockNum: null,
			 remark: null,
			 delFlag: null,
			 createBy: null,
			 createTime: null,
			 updateBy: null,
			 updateTime: null
        },
	  nickName:"",
	  // 校验规则
	  rules: {
	  	fruitsId: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '水果名称不能为空'
	  		}]
	  	},
	  	salesTime: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '进货日期不能为空'
	  		}]
	  	},
	  	stockNum: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '进货数量不能为空'
	  		}, {
	  			format: 'number',
	  			errorMessage: '进货数量只能输入数字'
	  		}]
	  	},
	  	purchasePrice: {
	  		rules: [{
	  			required: false,
	  			errorMessage: '进货价不能为空'
	  		}, {
	  			format: 'number',
	  			errorMessage: '进货价只能输入数字'
	  		}]
	  	},
	  	retailPrice: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '零售价不能为空'
	  		}, {
	  			format: 'number',
	  			errorMessage: '零售价只能输入数字'
	  		}]
	  	},
	  },
	  fruitsInfoList: [],
    }
  },
  onLoad (params) {
	this.getFruitsInfoList();
    this.id = params.id
    this.loadData()
  },
  methods: {
    loadData () {
      if (this.id) {
        const app = this
        FruitsStockInfoApi.getFruitsStockInfo(this.id).then(res => {
			if(res.data)
			{
				app.formData = res.data
			}
        })
      }
    },
	getFruitsInfoList()
	{
		DictInfoApi.getDicts("unit").then(res => {
			if(res.data)
			{
				FruitsInfoApi.listAll().then(response => {
					this.fruitsInfoList = [];
					if(response.rows)
					{
						if(response.rows.length)
						{
							for(let i=0;i<response.rows.length;i++)
							{
								let temp = response.rows[i];
								let obj = {};
								obj.value = temp.id;
								const dicts = res.data.find(i => i.dictValue === temp.unit)
								obj.text = temp.fruitsName+"（"+dicts.dictLabel+"）"
								this.fruitsInfoList.push(obj);
							}
						}
					}
				});
			}
		});
	},
	 
    del() {
		let id = this.id
		uni.showModal({
			title: '提示',
			content: '确定要删除吗？',
			success: function (res) {
				if (res.confirm) {
					FruitsStockInfoApi.delFruitsStockInfo(id).then(res2 => {
					  uni.showToast({ title: '提交成功！' })
					  uni.navigateTo({ url: '/pages/fruits/FruitsStockInfo/index'})
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
    },
    submit() {
		this.$refs.form.validate().then(res => {
		  if (this.id) {
		    FruitsStockInfoApi.updateFruitsStockInfo(this.formData).then(res => {
		      uni.showToast({ title: '提交成功！' })
					this.goBack()
		    })
		  } else {
		    FruitsStockInfoApi.addFruitsStockInfo(this.formData).then(res => {
		      uni.showToast({ title: '提交成功！' })
					this.goBack()
		    })
		  }
		});
    },
    goBack() {
		uni.navigateTo({ url: '/pages/fruits/FruitsStockInfo/index'})
    },
	calculateRetailPrice()
	{
		if(parseInt(this.formData.salesNum)>0)
		{
		  this.formData.retailPrice = this.formData.salesTotal/this.formData.salesNum
		}else{
		  this.formData.retailPrice = 0
		}
	}
  }
}
</script>
<style lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>
